package com.imooc.service.impl;

import com.imooc.bo.UserAddressBo;
import com.imooc.enums.YesOrNo;
import com.imooc.mapper.UserAddressMapper;
import com.imooc.pojo.UserAddress;
import com.imooc.service.UserAddressService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @author wangke
 * @version 1.0.0
 * @Description 用户的收获地址服务
 * @createTime 2021年08月01日 19:53:00
 */
@Service
public class UserAddressServiceImpl implements UserAddressService {
    @Autowired
    private UserAddressMapper addressMapper;
    @Autowired
    private Sid sid; //全局唯一主键工具

    @Override
    @Transactional(propagation=Propagation.SUPPORTS)
    public List<UserAddress> queryAllAddress(String userId) {
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        List<UserAddress> addresses = addressMapper.select(userAddress);
        return addresses;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addUserAddress(UserAddressBo addressBo) {
        //查询用户地址数据，如果为首个地址保存为默认地址，否则不操作
        Integer defaultAddress=0;
        String userId = addressBo.getUserId();
        List<UserAddress> addresses = queryAllAddress(userId);
        if(addresses==null||addresses.isEmpty()||addresses.size()==0){
            defaultAddress=1;
        }
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(addressBo,userAddress);
        userAddress.setIsDefault(defaultAddress);
        userAddress.setId(sid.nextShort());
        userAddress.setCreatedTime(new Date());
        userAddress.setUpdatedTime(new Date());
        addressMapper.insert(userAddress);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteUserAddress(String userId, String addressId) {
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        userAddress.setId(addressId);
        addressMapper.delete(userAddress);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateAddress(UserAddressBo addressBo) {
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(addressBo,userAddress);
        String addressId = addressBo.getAddressId();
        userAddress.setId(addressId);
        userAddress.setUpdatedTime(new Date());
        addressMapper.updateByPrimaryKeySelective(userAddress);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void setDefalutAddress(String userId, String addressId) {
        //设置默认收货地址，根据用户id先将所有默认取消，再根据默认地址id设置
        UserAddress userAddress = new UserAddress();
        Example example = new Example(UserAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        userAddress.setIsDefault(YesOrNo.no.type);
        addressMapper.updateByExampleSelective(userAddress,example);
        //设置默认地址
        userAddress.setIsDefault(YesOrNo.yes.type);
        userAddress.setUserId(userId);
        userAddress.setId(addressId);
        addressMapper.updateByPrimaryKeySelective(userAddress);
    }
}
